<?php

$uploaddir = getcwd() . \controller\Config::getPropertyByKey('directory.upload');

$acceptedFormats = array('gif', 'png', 'jpg', 'jpeg');

if (!init()) {
  redirect('?action=login');
}

$timeStamp = getLocalTimeStamp();
$resultSet = createArticleComment($_POST['nid'], $_POST['content'], f_Date($timeStamp), f_Time($timeStamp), $_POST['reply']);

$idRow = mysql_insert_id();


if (isset($_FILES['images'])) {
  $i = 0;

  $lastInsertRespImgId = 0;

  $uploadfile = '';

  while (isset($_FILES['images']['name'][$i])) {

    if (in_array(strtolower(pathinfo($_FILES['images']['name'][$i], PATHINFO_EXTENSION)), $acceptedFormats)) {
      $file = date("dmY") . "_" . date("GHi") . getFormattedTime() . "_resp_img" . "." . pathinfo($_FILES['images']['name'][$i], PATHINFO_EXTENSION);
      $uploadfile = $uploaddir . $file;


      if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $uploadfile)) {
        $imgInsertResult = saveNewArticleCommentImages($file, $idRow);
      } else echo $_FILES['images']['error'][$i];
    }
    $i++;
  }

}

$initiatorAction = \controller\ControllerUtil::getEncodedActionFromUrlWithParameters($_SERVER['HTTP_REFERER']);

if ($resultSet) {
  increaseUsersMessages();
  redirect("?action=msg&header=header.response.comment.success&body=body.response.comment.success&view=" . $initiatorAction);
} else {
  redirect("?action=msg&header=header.response.comment.failed&body=body.response.comment.failed&view=" . $initiatorAction);
}

?>